﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Mapeo;
using BE;
using NHibernate;
using NHibernate.Criterion;
using System.Data;
using MySql.Data.MySqlClient;



namespace DAL
{
    public class CajaDiaria_DAL : Controlador
    {
      public DataTable listado(String Codigo, String Nombre)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    MySqlCommand SqlCmd = new MySqlCommand();
                    SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                    Trans.Enlist(SqlCmd);
                    SqlCmd.CommandText = "sp_ListadoCajaDiaria";
                    SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCmd.Parameters.AddWithValue("@in_codigo", Codigo);
                    SqlCmd.Parameters.AddWithValue("@in_nombre", Nombre);


                    SqlCmd.ExecuteNonQuery();
                    MySqlDataAdapter da = new MySqlDataAdapter(SqlCmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    Trans.Commit();
                    return dt;
                }
            }
        }
      public void Insertar(CajaDiaria cp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        MySqlCommand SqlCmd = new MySqlCommand();
                        SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                        Trans.Enlist(SqlCmd);

                        SqlCmd.CommandText = "sp_ValidarCajaDiaria";
                        SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                        SqlCmd.Parameters.AddWithValue("@in_Caja", cp.idCAJA);

                        SqlCmd.Parameters.Add("@out_mensaje", MySqlDbType.Int32, 50);
                        SqlCmd.Parameters["@out_mensaje"].Direction = ParameterDirection.Output;

                        SqlCmd.ExecuteNonQuery();

                        Int32 mensaje = Convert.ToInt32(SqlCmd.Parameters["@out_mensaje"].Value);

                        if (mensaje > 0) {
                            throw new Exception("Ya existe una caja abierta con la caja seleccionada");
                        }


                        cp.fechaApertura = DateTime.Now;
                        cp.Activo = true;

                        Sesion.Save(cp);
                        Sesion.Flush();

                       

                        SqlCmd.CommandText = "sp_ActualizarSaldoCajaDiaria";
                        SqlCmd.Parameters.Clear();
                        SqlCmd.Parameters.AddWithValue("@in_IDCajaDiaria", cp.IDCp);
                        SqlCmd.Parameters.AddWithValue("@in_IDCaja", cp.idCAJA);
                        SqlCmd.ExecuteNonQuery();

                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }

      //public void ActualizarCierre(CajaDiaria cd)
      //{
      //    using (ISession Sesion = m_SessionFactory.OpenSession())
      //    {
      //        using (ITransaction Trans = Sesion.BeginTransaction())
      //        {
      //            try
      //            {
      //                Sesion.Update(cd);
      //                Sesion.Flush();

      //                MySqlCommand SqlCmd = new MySqlCommand();
      //                SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
      //                Trans.Enlist(SqlCmd);

      //                SqlCmd.CommandText = "sp_CierreCajaDiaria";
      //                SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
      //                SqlCmd.Parameters.AddWithValue("@in_IDCajaDiaria", cd.IDCp);
      //                SqlCmd.Parameters.AddWithValue("@in_IDCaja", cd.idCAJA);
      //                SqlCmd.Parameters.AddWithValue("@in_FechaApertura", cd.fechaApertura);
      //                SqlCmd.Parameters.AddWithValue("@in_FechaCierre", cd.FechaCierre);
      //                SqlCmd.Parameters.AddWithValue("@in_Activo", cd.Activo);
      //                SqlCmd.Parameters.AddWithValue("@in_SaldoInicial", cd.saldoInicial);
      //                SqlCmd.Parameters.AddWithValue("@in_SaldoFinalSistema", cd.SaldoFinalSistema);
      //                SqlCmd.Parameters.AddWithValue("@in_SaldoFinalReal", cd.SaldoFinalReal);
      //                SqlCmd.Parameters.AddWithValue("@in_observacion", cd.Observacion);
      //                SqlCmd.ExecuteNonQuery();

      //                Trans.Commit();
      //            }
      //            catch (Exception)
      //            {
      //                Trans.Rollback();
      //                throw;
      //            }
      //        }
      //    }
      //}

      public void ActualizaCierre_CajaDiaria( CajaDiaria cajadiaria)
      {
          using (ISession Sesion = m_SessionFactory.OpenSession())
          {
              using (ITransaction Trans = Sesion.BeginTransaction())
              {
                  try
                  {
                      Sesion.Update(cajadiaria);
                      Trans.Commit();
                  }
                  catch (Exception)
                  {
                      Trans.Rollback();
                      throw;
                  }
              }
          }
      }



      public void Eliminar(Int32 IDCajaDiaria)
      {
          using (ISession Sesion = m_SessionFactory.OpenSession())
          {
              using (ITransaction Trans = Sesion.BeginTransaction())
              {
                  try
                  {
                      object var = (Object)Sesion.CreateCriteria("CajaDiaria").Add(Restrictions.Eq("IDCp", IDCajaDiaria)).UniqueResult();
                      Sesion.Delete(var);
                      Trans.Commit();
                  }
                  catch (Exception)
                  {
                      Trans.Rollback();
                      throw;
                  }
              }
          }
      }


     public CajaDiaria ObetenerCajaDiariaPorID(Int32 IDCajaDiaria) 
      {
          using (ISession Sesion = m_SessionFactory.OpenSession())
          {
              using (ITransaction Trans = Sesion.BeginTransaction())
              {
                  try
                  {
                      object var = (Object)Sesion.CreateCriteria("CajaDiaria").Add(Restrictions.Eq("IDCp", IDCajaDiaria)).UniqueResult();
                      return (CajaDiaria)var;                                              
                  }
                  catch (Exception)
                  {
                      Trans.Rollback();
                      throw;
                  }
              }
          }
      }
     
    }
}
